648C - Путь Робота - CodeForces Solution


constructive algorithms dfs and similar graphs *1100

Please click on ads to support us..

Python Code:

n,m = map(int, input().split())

a = []
s_index = (0,0)
for i in range(n):
    row = list(input())
    try:
        s_index = (i, row.index('S'))
    except:
        pass
    a.append(row)

s = [s_index]
r = []
x,y = s_index

while(s):
    x,y = s.pop()
    if a[x][y]==0: continue
    if a[x][y]!='S':
        a[x][y] = 0

    for i,j, move in [(x-1, y, 'U'), (x, y+1, 'R'), (x+1, y, 'D'), (x, y-1, 'L')]:
        if i < n and j < m and i >= 0 and j >= 0 and (a[i][j]=='*'):
            s.append((i,j))
            r.append(move)
            break

for i,j, move in [(x-1, y, 'U'), (x, y+1, 'R'), (x+1, y, 'D'), (x, y-1, 'L')]:
    if i < n and j < m and i >= 0 and j >= 0 and (a[i][j]=='S'):
        r.append(move)
        break


print(*r, sep="")


Comments

Submit
0 Comments
More Questions

Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people